var _oFieldXmlDoc = CreateXmlDocument();
var _oFieldXslDoc = CreateXmlDocument(true);
var _oFieldXslt = CreateXslTemplate();

var _sSortColumnName = "displayname";
var _bSortOrderAscend = true;

var _oLastGridColumnOver = null;

function BuildFieldList()
{
_oFieldXslDoc.load("/Tools/FormEditor/fields.xsl");
_oFieldXslt.stylesheet = _oFieldXslDoc;
_oFieldXmlDoc.loadXML(GetFieldsXml());
UpdateFieldList(null, false);
}

function UpdateGridBar(sSortColumnName, bSortOrderAscend, bSelectAll)
{

var aCells = tblGridBar.cells;
for (var i = 0; i < aCells.length; i++)
{
var oCell = aCells[i];
if (oCell.className == "ViewForm_AddFields_th_gridheader")
{
var oImg = oCell.firstChild.children[0];
with (oImg.style)
{
visibility	= sSortColumnName == oCell.field ? "visible" : "hidden";
filter		= bSortOrderAscend ? "" : "flipv()";
oImg.alt	= sSortColumnName == oCell.field ? (bSortOrderAscend ? LOCID_ALT_COLUMNSORTORDER_UP:LOCID_ALT_COLUMNSORTORDER_DOWN ) : "";
}
if ( LOCID_UI_DIR=="RTL" )
{
if ( oImg.style.filter == "" )
oImg.style.filter = "fliph()" ;
else
oImg.style.filter += " fliph()" ;
}
}
}

if (typeof(chkAll)!="undefined")
{
chkAll.checked = (typeof(bSelectAll)!="undefined" && bSelectAll) ? true : false;
}
}

function UpdateFieldList(oFieldListInfo, bSaveRestoreState)
{
oFieldListInfo = "undefined"==typeof(oFieldListInfo) ? null : oFieldListInfo;

var oChecked = {};
if (bSaveRestoreState)
{
for (var i = 0; i < Fields.rows.length; i++)
{
var oRow = Fields.rows[i];
oChecked[oRow.cells[2].innerText] = oRow.cells[0].firstChild.checked;
}
}


if(!IsNull(oFieldListInfo))
{
_sSortColumnName = oFieldListInfo.sSortColumnName;
_bSortOrderAscend = oFieldListInfo.bSortOrderAscend;
var bSelectAll = oFieldListInfo.bSelectAll;

FieldList.innerHTML = oFieldListInfo.sHTML;
}
else
{


var oFieldXslProc = _oFieldXslt.createProcessor();
oFieldXslProc.input = _oFieldXmlDoc;
oFieldXslProc.addParameter("sortColumnName", _sSortColumnName);
oFieldXslProc.addParameter("languageName", XML_LANGUAGE_NAME);
oFieldXslProc.addParameter("sortOrder", _bSortOrderAscend ? "ascending" : "descending");
oFieldXslProc.addParameter("RTL", LOCID_UI_DIR=="RTL" ? "true" : "false");
oFieldXslProc.transform();
FieldList.innerHTML = oFieldXslProc.output;
}


UpdateGridBar(_sSortColumnName, _bSortOrderAscend, bSelectAll);


if (bSaveRestoreState)
{
for (var i = 0; i < Fields.rows.length; i++)
{
var oRow = Fields.rows[i];
oRow.cells[0].firstChild.checked = oChecked[oRow.cells[2].innerText];
}
}
}


function onGridColumnClick()
{
var o = event.srcElement;

switch (o.tagName)
{
case "IMG": return;
case "NOBR": o = o.parentElement;
}


if (o.className == "ViewForm_AddFields_th_gridheader")
{
if (o.field == _sSortColumnName)
{
_bSortOrderAscend = !_bSortOrderAscend;
}
else
{
_sSortColumnName = o.field;
_bSortOrderAscend = true;
}
}


UpdateFieldList(null, true);
}


function onGridColumnMouseOver()
{
var o = event.srcElement;

switch (o.tagName)
{
case "IMG": return;
case "NOBR": o = o.parentElement;
}


if (o.className == "ViewForm_AddFields_th_gridheader" )
{
if(  o.runtimeStyle.backgroundImage != "url(/_imgs/grid/grid_BarBkOn.gif)" )
{
o.runtimeStyle.backgroundImage = "url(/_imgs/grid/grid_BarBkOn.gif)";
_oLastGridColumnOver = o;
}


if ( o.title == "" && o.innerText != "")
{
o.title = o.innerText;
}
}
}


function onGridColumnMouseOut()
{
var o = event.toElement;


if (!IsNull(_oLastGridColumnOver) && !(!IsNull(o) && (o == _oLastGridColumnOver || o.parentElement == _oLastGridColumnOver || o.parentElement.parentElement == _oLastGridColumnOver)))
{
_oLastGridColumnOver.runtimeStyle.backgroundImage = "";
_oLastGridColumnOver = null;
}
}
